tileset= {}
tileset.texture = game.images:load("images/World3.png")
tileset.texture:resize(32,32)
tileset.texSizeX = 256
tileset.texSizeY = 1152

function drawTile(posX, posY, tileX, tileY, image)
	local texPartX = (1 / image.texSizeX) * 32
	local texPartY = (1 / image.texSizeY) * 32
	image.texture:drawPosSub(posX, posY,tileX*texPartX, tileY*texPartY,texPartX,texPartY)
end

function createMap(sizeX, sizeY)
	local map = {}
	map.sizeX = sizeX
	map.sizeY = sizeY
	for i=0, sizeY-1 do
	map[i] = {}
		for j=0, sizeX-1 do
		map[i][j] = {}
		map[i][j] = {x = -1, y = -1}
		end
	end
	return map
end

function createGrassMap(sizeX, sizeY)
	local map = {}
	map.sizeX = sizeX
	map.sizeY = sizeY
	for i=0, sizeY-1 do
	map[i] = {}
		for j=0, sizeX-1 do
		map[i][j] = {}
		map[i][j] = {x = 0, y = 35}
		end
	end
	return map
end

function createLayers(n, sizeX, sizeY)
	local layers = {sizeX = sizeX, sizeZ = sizeZ, count = n}
	layers[0] = {}
	layers[0] = createGrassMap(sizeX, sizeY)
	for i=1,n do
		layers[i] = {}
		layers[i] = createMap(sizeX, sizeY)
	end
	return layers	
end

--map must be of type map[sizeY][sizeX] and must have the attributes map.sizeX, map.SizeY
function drawMap(posX, posY, map, image)
	for i=0,map.sizeY-1 do
		if(i*32+posY <632 and i*32+posY>-32) then
		for j=0,map.sizeX-1 do
			--prevents the drawing of tiles offscreen to gain much more performance
			if(j*32+posX<832 and j*32+posX>-32) then
				if(map[i]) then
					if(map[i][j]) then
						if(map[i][j].x>-1 and map[i][j].y>-1) then
						drawTile(32*j+posX, 32*i+posY, map[i][j].x, map[i][j].y, image)
						end
					end
				end
			else break end
		end
		else break end
	end
end

function drawLayers(posX, posY, layers, image)
	for i=0,layers.count do
		drawMap(posX, posY, layers[i], image)
	end
end

function setTile(map, mapPosX, mapPosY, newX, newY)
	map[mapPosY][mapPosX] = {x = newX, y = newY}
end